package BandB.Tool.GPhoneManager.TaskManager.Util;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;

public class DataHandler {
	// public static final byte[] Sync

	private Context mContext;
	// private ArrayList<SimpleAppInfo> mBlacklistAppInfo;
	private static ArrayList<String> SystemProcessList;

	public DataHandler(Context c) {
		mContext = c;
		// mBlacklistAppInfo = new ArrayList<SimpleAppInfo>();
		SystemProcessList = new ArrayList<String>();
		SystemProcessList.add("com.android.phone");
		SystemProcessList.add("com.android.launcher");
		SystemProcessList.add("com.android.mms");
		SystemProcessList.add("com.android.alarmclock");
		SystemProcessList.add("android.process.media");
		SystemProcessList.add("com.google.android.backup");
	}

	public boolean IsSystemProcess(String packageName) {
		if (SystemProcessList.contains(packageName)) {
			return true;
		} else {
			return false;
		}
	}

	public ArrayList<SimpleAppInfo> getSimpleInfoOfBlacklist() {
		Cursor cursor = null;
		ArrayList<SimpleAppInfo> blackListAppInfo = new ArrayList<SimpleAppInfo>();
		if (mContext instanceof Activity) {
			cursor = ((Activity) mContext).managedQuery(
					DataProvider.APPLICATIONS_CONTENT_URI,
					DataProvider.APP_PROJECTION, null, null, null);
		} else {
			cursor = mContext.getContentResolver().query(
					DataProvider.APPLICATIONS_CONTENT_URI,
					DataProvider.APP_PROJECTION, null, null, null);
		}
		if (null != cursor && cursor.moveToFirst()) {
			do {
				cursor.getCount();
				SimpleAppInfo info = new SimpleAppInfo();
				info.packageName = cursor.getString(0);
				info.appName = cursor.getString(1);
				blackListAppInfo.add(info);
			} while (cursor.moveToNext());
		}
		if (!(mContext instanceof Activity) && null != cursor) {
			cursor.close();
		}
		return blackListAppInfo;
	}

	public boolean isBlack(String packageName) {
		boolean result = false;
		Cursor cursor = null;
		if (mContext instanceof Activity) {
			cursor = ((Activity) mContext).managedQuery(
					DataProvider.APPLICATIONS_CONTENT_URI,
					new String[] { DataProvider.APP_PACKAGE_NAME },
					DataProvider.APP_PACKAGE_NAME + "='" + packageName + "'",
					null, null);
		} else {
			cursor = mContext.getContentResolver().query(
					DataProvider.APPLICATIONS_CONTENT_URI,
					new String[] { DataProvider.APP_PACKAGE_NAME },
					DataProvider.APP_PACKAGE_NAME + "='" + packageName + "'",
					null, null);
		}
		if (null != cursor && cursor.moveToFirst()) {
			result = true;
		}
		if (!(mContext instanceof Activity) && null != cursor) {
			cursor.close();
		}
		return result;
	}

	/**
	 * Move a application to blacklist
	 * 
	 * @param packageName
	 *            package of the application
	 * @return true if successful, false if the application is of system
	 * @throws NameNotFoundException
	 */
	public boolean moveToBlacklist(String packageName)
			throws NameNotFoundException {
		if (SystemProcessList.contains(packageName)) {
			return false;
		}

		ContentValues values = new ContentValues();
		PackageManager pManager = mContext.getPackageManager();
		ApplicationInfo info = pManager.getApplicationInfo(packageName, 0);
		values.put(DataProvider.APP_PACKAGE_NAME, packageName);
		values.put(DataProvider.APP_NAME, info.loadLabel(pManager).toString());

		mContext.getContentResolver().insert(
				DataProvider.APPLICATIONS_CONTENT_URI, values);
		return true;
	}

	public int removeFromBlacklist(String packageName) {
		return mContext.getContentResolver().delete(
				DataProvider.APPLICATIONS_CONTENT_URI,
				DataProvider.APP_PACKAGE_NAME + "='" + packageName + "'", null);
	}

}
